package com.gzceb.zhgjj.security.dao;

// default package

import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gzceb.zhgjj.security.entity.SysResources;
import com.gzceb.zhgjj.util.Utils;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysResources entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .SysResources
 * @author MyEclipse Persistence Tools
 */

public class SysResourcesDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(SysResourcesDAO.class);
	// property constants
	public static final String RESOURCE_NAME = "resourceName";
	public static final String RESOURCE_DESC = "resourceDesc";
	public static final String RESOURCE_TYPE = "resourceType";
	public static final String RESOURCE_STRING = "resourceString";
	public static final String PRIORITY = "priority";
	public static final String ENABLED = "enabled";
	public static final String ISSYS = "issys";
	public static final String MODULE = "module";

	protected void initDao() {
		// do nothing
	}

	/*
	 * 持久化资源。
	 */
	public void persist(SysResources transientInstance) {

		log.debug("持久化一个资源实例！");
		try {

			transientInstance.setResourceId(Utils.getPkId() + "");

			getHibernateTemplate().persist(transientInstance);

			log.debug("持久化资源实例成功！");

		} catch (RuntimeException re) {
			log.error("持久化资源实例失败！", re);
			throw re;
		}
	}

	public void save(SysResources transientInstance) {
		log.debug("saving SysResources instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * public void delete(SysResources persistentInstance) {
	 * log.debug("deleting SysResources instance"); try {
	 * getHibernateTemplate().delete(persistentInstance);
	 * log.debug("delete successful"); } catch (RuntimeException re) {
	 * log.error("delete failed", re); throw re; } }
	 */

	/*
	 * 根据资源对象删除一个对象，非物理删除，而是一个逻辑删除，置enabled为false。
	 */
	public void delete(SysResources resource) {
		log.debug("根据一个资源id删除一个用户，资源id为：" + resource.getResourceId());
		try {

			// SysResources resource = findById( resourceId );

			resource.setEnabled(0);
			// getHibernateTemplate().saveOrUpdate( resource );

			log.debug("删除成功！");
		} catch (RuntimeException re) {

			log.error("删除失败！", re);
			throw re;
		}
	}

	/*
	 * 根据资源id删除一个资源，非物理删除，而是一个逻辑删除，置enabled为false。
	 */
	public void delete(String resourceId) {
		log.debug("根据一个资源id删除一个用户，资源id为：" + resourceId);
		try {

			SysResources resource = findById(resourceId);

			resource.setEnabled(0);
			// getHibernateTemplate().saveOrUpdate( resource );

			log.debug("删除成功！");
		} catch (RuntimeException re) {

			log.error("删除失败！", re);
			throw re;
		}
	}

	public SysResources findById(java.lang.String id) {
		log.debug("getting SysResources instance with id: " + id);
		try {
			SysResources instance = (SysResources) getHibernateTemplate().get(
					"SysResources", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SysResources instance) {
		log.debug("finding SysResources instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysResources instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from SysResources as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByResourceName(Object resourceName) {
		return findByProperty(RESOURCE_NAME, resourceName);
	}

	public List findByResourceDesc(Object resourceDesc) {
		return findByProperty(RESOURCE_DESC, resourceDesc);
	}

	public List findByResourceType(Object resourceType) {
		return findByProperty(RESOURCE_TYPE, resourceType);
	}

	public List findByResourceString(Object resourceString) {
		return findByProperty(RESOURCE_STRING, resourceString);
	}

	public List findByPriority(Object priority) {
		return findByProperty(PRIORITY, priority);
	}

	public List findByEnabled(Object enabled) {
		return findByProperty(ENABLED, enabled);
	}

	public List findByIssys(Object issys) {
		return findByProperty(ISSYS, issys);
	}

	public List findByModule(Object module) {
		return findByProperty(MODULE, module);
	}

	public List findAll() {
		log.debug("finding all SysResources instances");
		try {
			String queryString = "from SysResources";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * 得到资源列表，参数列表在实例中包含着，包括资源号、资源名称、资源类型、资源地址。
	 */
	public List<SysResources> findResourcesLst(SysResources resource) {

		List<SysResources> list = null;
		String sql = "from SysResources where enabled=1 ";
		try {
			if (!"".equals(resource.getResourceName())) {
				sql += "and resource_name like '" + resource.getResourceName()
						+ "' ";
			}

			if (!"".equals(resource.getResourceDesc())) {
				sql += "and resource_desc like '" + resource.getResourceDesc()
						+ "' ";
			}

			if (!"".equals(resource.getResourceType())) {
				sql += "and resource_type = '" + resource.getResourceType()
						+ "' ";
			}

			if (!"".equals(resource.getResourceString())) {
				sql += "and resource_string like '%"
						+ resource.getResourceString() + "%' ";
			}

			list = getHibernateTemplate().find(sql);

		} catch (RuntimeException re) {
			log.error("findByUserAccount() 错误！", re);
			throw re;
		}

		return list;

	}

	public SysResources merge(SysResources detachedInstance) {
		log.debug("merging SysResources instance");
		try {
			SysResources result = (SysResources) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SysResources instance) {
		log.debug("attaching dirty SysResources instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SysResources instance) {
		log.debug("attaching clean SysResources instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static SysResourcesDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (SysResourcesDAO) ctx.getBean("SysResourcesDAO");
	}
}